// 此文件为单文件版，即每种语言都有自己的js文件
const fs = require("fs")
const path = require('path')
const file = path.resolve(__dirname, './文字.txt')
const text = require("./messages.js")
const axios = require('axios')
const md5 = require('md5')
const qs = require('qs')
// &from=&to=en&text=考勤计算

function waitTime(time = 1500){
    return new Promise((res) => {
        setTimeout(() => {
            res()
        }, time);
    })
}

let url = 'https://www.iciba.com/word?w='

// url = `https://fanyi.youdao.com/translate?&doctype=json&type=cn&i=`
url = `http://fanyi.youdao.com/translate?&doctype=json&type=ZH_CN2EN&i=`
let params = {
    'doctype': 'json', 
    'type': 'auto',
    'i': '你吃饭了吗？'
}
// 翻译
function translate(word){
    return new Promise(async (res, rej) => {
        await waitTime(50)
        try{
            params.i = word
            let {data} = await axios.get(`${url}${encodeURI(word)}`)
            // let txt = getDom(data, word)
            // res(txt)
            res(data.translateResult[0][0].tgt)
        }catch(err){
            rej(err)
        }
    })

}

function googleTranslate(word){
    return new Promise(async (res, rej) => {
        await waitTime(50)
        try{
            let url = `http://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl=zh_TW&q=${word}`
            let {data} = await axios.get(url)
            params.i = word
            
            console.log(data, '结果')
            // res(data.translateResult[0][0].tgt)
        }catch(err){
            rej(err)
        }
    })

}

// 递归函数
function revursion(node){
    for(let k in node){
        if(typeof node[k] == "object"){
            fs.writeFile(file, '\n' + '\n' + k + '\n' + '\n',{flag: 'a'},err => {})
            revursion(node[k])
        }else{
            translate(node[k])
            fs.writeFile(file, node[k] + '\n',{flag: 'a'},err => {})
        }
    }
}


async function deepTranslate2(node){
    let res = node.constructor === Array ? [] : {};
    for(let k in node){
        if(typeof node[k] === 'string'){
            res[k] = await baiduFY(node[k])
        }else if(typeof node[k] === 'object'){
            res[k] = await deepTranslate2(node[k])
        }else{
            console.log('出现不是字符串也不是对象类型！！！', node[k] )
        }
    }
    return res
};

// 百度翻译
async function baiduFY(wrod){
    const salt = Math.random();
    let url = `http://api.fanyi.baidu.com/api/trans/vip/translate` //百度
    let params = { 
        q: wrod,
        from: 'en',
        to: 'fid',
        appid: '20220103001045602',
        salt,
        sign: ''
    }
    params.sign = md5(`${params.appid}${params.q}${params.salt}ryDj3OHS0nca7vj3MFNG`)
    params.q = encodeURIComponent(params.q)
    console.log(params)
    let rs = await axios.get(url, {
        params
    })
   console.log(rs.data, '翻译结果')
}
(async () => {
    let res  = await deepTranslate2(JSON.parse(JSON.stringify(text)))
    console.log(res, '最终结果')
    fs.writeFile(`./res/result${Date.now()}.json`, JSON.stringify(res), err => {
        if(err){
            console.log('写入文件失败')
        }else{
            console.log('写入文件成功')
        }
    })
})();